package org.jeecg.modules.common;

import java.awt.*;
import java.util.HashSet;
import java.util.Set;

public class FileTypeChecker {

    private static final Set<String> IMAGE_EXTENSIONS = new HashSet<>();
    private static final Set<String> VIDEO_EXTENSIONS = new HashSet<>();
    private static final Set<String> AUDIO_EXTENSIONS = new HashSet<>();
    private static final Set<String> DOCUMENT_EXTENSIONS = new HashSet<>();

    static {
        // 常见图片文件扩展名
        IMAGE_EXTENSIONS.add("jpg");
        IMAGE_EXTENSIONS.add("jpeg");
        IMAGE_EXTENSIONS.add("png");
        IMAGE_EXTENSIONS.add("gif");
        IMAGE_EXTENSIONS.add("bmp");

        // 常见视频文件扩展名
        VIDEO_EXTENSIONS.add("mp4");
        VIDEO_EXTENSIONS.add("avi");
        VIDEO_EXTENSIONS.add("mkv");
        VIDEO_EXTENSIONS.add("mov");
        VIDEO_EXTENSIONS.add("wmv");
        VIDEO_EXTENSIONS.add("flv");
        VIDEO_EXTENSIONS.add("mpeg");
        VIDEO_EXTENSIONS.add("mpg");


        // 常见音频文件扩展名
        AUDIO_EXTENSIONS.add("mp3");
        AUDIO_EXTENSIONS.add("wav");
        AUDIO_EXTENSIONS.add("ogg");
        AUDIO_EXTENSIONS.add("flac");
        AUDIO_EXTENSIONS.add("aac");

        // 常见文档文件扩展名
        DOCUMENT_EXTENSIONS.add("doc");
        DOCUMENT_EXTENSIONS.add("docx");
        DOCUMENT_EXTENSIONS.add("xls");
        DOCUMENT_EXTENSIONS.add("xlsx");
        DOCUMENT_EXTENSIONS.add("ppt");
        DOCUMENT_EXTENSIONS.add("pptx");
        DOCUMENT_EXTENSIONS.add("pdf");
        DOCUMENT_EXTENSIONS.add("txt");
    }

    public static FileType checkFileType(String fileName) {
        String extension = getFileExtension(fileName.toLowerCase());

        if (IMAGE_EXTENSIONS.contains(extension)) {
            return FileType.IMAGE;
        } else if (VIDEO_EXTENSIONS.contains(extension)) {
            return FileType.VIDEO;
        } else if (AUDIO_EXTENSIONS.contains(extension)) {
            return FileType.AUDIO;
        } else if (DOCUMENT_EXTENSIONS.contains(extension)) {
            return FileType.AUDIO;
        } else {
            return FileType.OTHER;
        }
    }

    private static String getFileExtension(String fileName) {
        int lastDotIndex = fileName.lastIndexOf(".");
        if (lastDotIndex != -1 && lastDotIndex < fileName.length() - 1) {
            return fileName.substring(lastDotIndex + 1);
        } else {
            return "";
        }
    }

    public static void main(String[] args) {
        String imageFile = "example.jpg";
        String videoFile = "example.mp4";
        String audioFile = "example.mp3";
        String unknownFile = "example.txt";

        System.out.println("Image file type: " + checkFileType(imageFile));
        System.out.println("Video file type: " + checkFileType(videoFile));
        System.out.println("Audio file type: " + checkFileType(audioFile));
        System.out.println("Unknown file type: " + checkFileType(unknownFile));
    }

    public enum FileType {
        IMAGE("1"),
        VIDEO("2"),
        AUDIO("3"),
        DOCUMENT("4"),
        OTHER("10");
        private String value;
        private FileType(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public static boolean isIMAGE(String value){
            return value==null?false:FileType.IMAGE.value.equals(value);
        }

        public static boolean isVIDEO(String value){
            return value==null?false:FileType.VIDEO.value.equals(value);
        }

        public static boolean isAUDIO(String value){
            return value==null?false:FileType.AUDIO.value.equals(value);
        }
        public static boolean isDOCUMENT(String value){
            return value==null?false:FileType.DOCUMENT.value.equals(value);
        }
    }
}

